package com.inspur.project.system.service.impl;

import java.util.List;

import com.inspur.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.inspur.project.system.mapper.SysDistrictMapper;
import com.inspur.project.system.domain.SysDistrict;
import com.inspur.project.system.service.ISysDistrictService;

/**
 * 区域信息Service业务层处理
 *
 * @author inspur
 * @date 2020-03-14
 */
@Service
public class SysDistrictServiceImpl implements ISysDistrictService
{
    @Autowired
    private SysDistrictMapper sysDistrictMapper;

    /**
     * 查询区域信息
     *
     * @param id 区域信息ID
     * @return 区域信息
     */
    @Override
    public SysDistrict selectSysDistrictById(Long id) {
        return sysDistrictMapper.selectSysDistrictById(id);
    }

    @Override
    public SysDistrict selectSysDistrictByCode(String code) {
        return sysDistrictMapper.selectSysDistrictByCode(code);
    }

    @Override
    public SysDistrict selectSysDistrictByParentId(Long parentId) {
        return sysDistrictMapper.selectSysDistrictByParentId(parentId);
    }

    @Override
    public SysDistrict selectSysDistrictByPCode(String code)
    {
        return sysDistrictMapper.selectSysDistrictByPCode(code);
    }

    @Override
    public int count(SysDistrict sysDistrict) {
        return sysDistrictMapper.count(sysDistrict);
    }

    /**
     * 查询区域信息列表
     *
     * @param sysDistrict 区域信息
     * @return 区域信息
     */
    @Override
    public List<SysDistrict> selectSysDistrictList(SysDistrict sysDistrict)
    {
        return sysDistrictMapper.selectSysDistrictList(sysDistrict);
    }

    /**
     * 新增区域信息
     *
     * @param sysDistrict 区域信息
     * @return 结果
     */
    @Override
    public int insertSysDistrict(SysDistrict sysDistrict)
    {
        SysDistrict parentDistrict = sysDistrictMapper.selectSysDistrictById(sysDistrict.getParentId());
        if(StringUtils.isNotEmpty(parentDistrict.getAncestors())) {
            sysDistrict.setAncestors(parentDistrict.getAncestors() + "," + sysDistrict.getPcode());
        }else {
            sysDistrict.setAncestors(sysDistrict.getPcode());
        }
        return sysDistrictMapper.insertSysDistrict(sysDistrict);
    }

    /**
     * 修改区域信息
     *
     * @param sysDistrict 区域信息
     * @return 结果
     */
    @Override
    public int updateSysDistrict(SysDistrict sysDistrict)
    {
        return sysDistrictMapper.updateSysDistrict(sysDistrict);
    }

    /**
     * 批量删除区域信息
     *
     * @param codes 需要删除的区域信息ID
     * @return 结果
     */
    @Override
    public int deleteSysDistrictByIds(String[] codes)
    {
        return sysDistrictMapper.deleteSysDistrictByIds(codes);
    }

    /**
     * 删除区域信息信息
     *
     * @param code 区域信息ID
     * @return 结果
     */
    @Override
    public int deleteSysDistrictById(String code)
    {
        return sysDistrictMapper.deleteSysDistrictById(code);
    }
}
